package com.hzw.saas.service.user.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzw.saas.api.user.ICustomerService;
import com.hzw.saas.api.user.IEmailService;
import com.hzw.saas.api.user.IStaffService;
import com.hzw.saas.api.user.bo.EmailBO;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.service.user.model.Customer;
import com.hzw.saas.service.user.model.Staff;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sonam
 * @sine 2021/9/28 4:04 下午
 */
@Service
@RequiredArgsConstructor
public class EmailServiceImpl implements IEmailService {

    private final ICustomerService customerService;
    private final IStaffService staffService;

    @Override
    public IPage<EmailBO> listEmails(PageParam pageParam, String keyword, Integer type) {
        if (StrUtil.isBlank(keyword)) {
            return new Page<>();
        }
        // 客户信息分页
        Page<Customer> customerPage = new Page<>();
        if(type == null || type == 1) {
            customerPage = ((CustomerServiceImpl) customerService).lambdaQuery()
                .likeRight(Customer::getCustomerName, keyword).or()
                .likeRight(Customer::getEmail, keyword).page(pageParam.convert());
        }
        if (customerPage != null && CollectionUtil.isEmpty(customerPage.getRecords()) && type == 1) {
            return new Page<>();
        }
        // 销售信息分页
        Page<Staff> staffPage = new Page<>();
        if(type == null || type == 2) {
            staffPage = ((StaffServiceImpl) staffService).lambdaQuery()
                .likeRight(Staff::getStaffName, keyword).or()
                .likeRight(Staff::getStaffEmail, keyword).page(pageParam.convert());
        }
        if (staffPage != null && CollectionUtil.isEmpty(staffPage.getRecords()) && type == 2) {
            return new Page<>();
        }

        // 查询总数量
        long total = type == null ? (staffPage.getTotal() + customerPage.getTotal()) :
            type == 1 ? customerPage.getTotal() :
                type == 2 ? staffPage.getTotal() : 0L;

        IPage<EmailBO> emails = new Page<>();
        emails.setRecords(new ArrayList<>());
        emails.setTotal(total);
        emails.setSize(pageParam.getSize());
        emails.setCurrent(pageParam.getCurrent());

        if(customerPage != null) {
            customerPage.getRecords().forEach(record -> {
                EmailBO email = new EmailBO();
                email.setEmail(record.getEmail());
                email.setRealName(record.getCustomerName());
                email.setUserId(record.getUserId());
                emails.getRecords().add(email);
            });
        }

        if(staffPage != null) {
            staffPage.getRecords().forEach(record -> {
                EmailBO email = new EmailBO();
                email.setEmail(record.getStaffEmail());
                email.setRealName(record.getStaffName());
                email.setUserId(record.getPid());
                emails.getRecords().add(email);
            });
        }

        if(emails.getRecords().size() > emails.getSize()) {
            List<EmailBO> EmailBOS = new ArrayList<>();
            for(int i = 0 ; i < emails.getSize() / 2; i ++) {
                EmailBOS.add(emails.getRecords().get(i));
            }
            for(int i = emails.getRecords().size()/2 ; i < emails.getSize() - emails.getSize() / 2; i ++) {
                EmailBOS.add(emails.getRecords().get(i));
            }
            emails.setRecords(EmailBOS);
        }

        return emails;
    }
}
